package com.pc.admin.config;

import com.pc.admin.annotation.AutoFrequency;
import com.pc.admin.strategy.FrequencyStrategy;
import com.pc.admin.strategy.factory.FrequencyFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
/**
 * Spring扩展接口统一配置中心
 *
 * @author <a href="https://www.lvfeiy.top">lfy</a>
 * @date 2023/6/23 22:06
 */
@Configuration
@Slf4j
public class GlobalConfig implements BeanPostProcessor,
        ApplicationRunner,
        ApplicationContextAware

{
    private ApplicationContext applicationContext;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if(bean instanceof FrequencyStrategy && bean.getClass().isAnnotationPresent(AutoFrequency.class)){
            FrequencyFactory.getInstance().register((FrequencyStrategy) bean);
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("===================卷完给你安排十个婆娘===========================");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
